<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Tab;
use App\Models\Media;
use App\Models\Device;
use App\Models\Company;
use Encore\Admin\Controllers\HasResourceActions;
use Log;
use Faker\Factory;
use Dcat\Admin\Widgets\Dropdown;
use Dcat\Admin\Widgets\Card;
use App\Admin\Renderable\DeviceTable;
use App\Admin\Extensions\MediaCheckRowController;


class MediaAuditController extends Controller
{

    public function index(Content $content)
    {
        return $content
            ->header('审核列表')
            ->description()
            ->body($this->grid());
    }
    public function grid(){
        Admin::script("
            $('.btn-secondary').on('click', function () {
                var content = '/uploads/'+$(this).data('content');
                console.log(content)
                layer.open({
                  type: 2,
                  title: false,
                  area: ['630px', '360px'],
                  shade: 0.8,
                  closeBtn: 0,
                  shadeClose: true,
                  content: content
                });
            });");
        return new Grid(Media::where(['status'=>0,'is_user'=>1]), function (Grid $grid) {
            // $grid->disableColumnSelector();
            $grid->column('id','ID');
            $grid->column('title','标题');
            $grid->column('remark','备注');
            $grid->column('src','路径')->display(function($src,$column){
                if ($this->type==1) {
                    return <<<EOT
<button type="button"
    class="btn btn-secondary"
    title="popover"
    data-container="body"
    data-toggle="popover"
    data-content="{$this->src}"
    >
  点击观看
</button>
EOT;
                }
                return $column->image();
            });
            // $grid->column('src','路径')->image();
            $grid->column('created_at','创建时间');
            $grid->actions(new MediaCheckRowController());
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // 当前行的数据数组
                $rowArray = $actions->row->toArray();
                // 当前行的某个字段的数据
                // $email = $actions->row->email;
                // 获取当前行主键值
                $id = $actions->getKey();
                // $actions->prepend('<a href="">通过</a>');
            });
            // 启用边框模式
            $grid->withBorder();
            $grid->fixColumns(2);
            //显示详情按钮
            // $grid->showViewButton();
            $grid->disableViewButton();
            //操作按钮
            $grid->showActions();
            // $grid->disableActions();
            // $grid->disableBatchDelete();
            // 创建按钮
            // $grid->showCreateButton();
            // 禁用创建按钮
            $grid->disableCreateButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            // 禁用编辑按钮
            $grid->disableEditButton();
            // 显示编辑按钮
            // $grid->showEditButton();
            // 禁用快捷编辑按钮
            $grid->disableQuickEditButton();
            // 显示快捷编辑按钮
            // $grid->showQuickEditButton();
            // 设置弹窗宽高，默认值为 '700px', '670px'
            // $grid->setDialogFormDimensions('50%', '60%');
            $grid->enableDialogCreate();
            // 禁用删除按钮
            $grid->disableDeleteButton();
            // 显示删除按钮
            // $grid->showDeleteButton();
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('type', '选择类型', ['图片','视频']);
            });
        });
    }
    public function update($id){
        return $this->form()->update($id);
    }
    public function form(){
        return Form::make(new Media, function (Form $form) {
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(9, 3);
                $form->column(6, function (Form\BlockForm $form) {
                    $form->text('title','媒体名称')->required();
                    // $u = Admin::user()->id==1?1:2; //1平台 2用户自传
                    // $form->hidden('is_user')->value($u);
                    $form->hidden('user_id')->value(Admin::user()->id);
                    $form->text('remark','媒体说明');
                    $form->select('type', '媒体类型')->options([
                        1 => '图片',
                        2 => '视频',
                    ]);
                    $form->file('src','平面图')->removable(false)->autoUpload();
                    $form->hidden('status')->customFormat(function ($v) {
                            return $v == '打开' ? 1 : 0;
                        })->saving(function ($v) {
                            return $v ? 1 : 0;
                        });
                });
            });
            // $form->saving(function (Form $form){

            // });
        });
    }
}